package org.esk.dablog.web.forms;

import org.acegisecurity.annotation.Secured;
import org.apache.commons.lang.StringUtils;
import org.esk.dablog.service.EntryManager;
import org.esk.dablog.web.util.RequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class
 * User: jc
 * Date: 22.11.2006
 * Time: 12:32:34
 * $Id:$
 */
@Secured({"ROLE_SUPERVISOR"})
public class DeleteEntryForm extends AbstractController {

    private EntryManager entryManager;   

    public EntryManager getEntryManager() {
        return entryManager;
    }

    public void setEntryManager(EntryManager entryManager) {
        this.entryManager = entryManager;
    }


    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse httpServletResponse) throws Exception {
        long entryId = RequestUtils.extractLongParameter(request);
        String redirectTo=request.getParameter("redirectTo");
        if(StringUtils.isEmpty(redirectTo)) {
            redirectTo="/";
        }
        entryManager.deleteEntry(entryId);        
        return new ModelAndView(new RedirectView(redirectTo));
    }


}
